package com.ssd.controller;

import cn.hutool.core.util.IdUtil;
import com.ssd.api.payment.NativePayApi;
import com.ssd.api.payment.RefundRecordApi;
import com.ssd.api.payment.TradingApi;
import com.ssd.api.payment.dto.enums.PayChannelEnum;
import com.ssd.api.payment.dto.request.NativePayReqDTO;
import com.ssd.api.payment.dto.response.ExecutionResultResDTO;
import com.ssd.api.payment.dto.response.NativePayResDTO;
import com.ssd.api.payment.dto.response.TradingResDTO;
import com.ssd.utils.IdUtils;
import com.ssd.view.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/feign")
@Api(tags = "远程调用测试控制层")
public class FeignController {
    @Resource// Native支付：获取二维码
    private NativePayApi nativePayApi;
    @Resource//退款服务接口：统一收单交易退款接口
    private RefundRecordApi refundRecordApi;
    @Resource//交易单服务接口：根据交易单号查询交易单的交易结果
    private TradingApi tradingApi;

    @GetMapping("/native/{state}")
    @ApiOperation("获取支付二维码")
    public BaseResponse getNativePayApi(@ApiParam(value = "支付渠道：1微信 0支付宝") @PathVariable("state") Integer state){
        NativePayReqDTO nativePayReqDTO = new NativePayReqDTO();
        nativePayReqDTO.setMemo("远程调用测试支付服务！");//备注
        nativePayReqDTO.setEnterpriseId(state==1?1561414331L:2088241317544335L);//商户号
        nativePayReqDTO.setProductAppId("user-service");//用户服务
        nativePayReqDTO.setProductOrderNo(IdUtils.getSnowflake().nextId());//业务订单号
        nativePayReqDTO.setTradingChannel(state==1?PayChannelEnum.WECHAT_PAY:PayChannelEnum.ALI_PAY);//支付渠道
        nativePayReqDTO.setTradingAmount(BigDecimal.valueOf(0.01));//交易金额
        nativePayReqDTO.setChangeChannel(false);//是否切换渠道
        NativePayResDTO downLineTrading = nativePayApi.createDownLineTrading(nativePayReqDTO);
        return new BaseResponse(downLineTrading);
    }

    @GetMapping("/refund/record")
    @ApiOperation("交易退款")
    public BaseResponse getRefundRecord(@ApiParam(value = "交易单号") @RequestParam("tradingOrderNo") Long tradingOrderNo,
                                         @ApiParam(value = "退款金额") @RequestParam("refundAmount") BigDecimal refundAmount){
        ExecutionResultResDTO executionResultResDTO = refundRecordApi.refundTrading(tradingOrderNo, refundAmount);
        return new BaseResponse(executionResultResDTO);
    }
    @GetMapping("/refund")
    @ApiOperation("查询交易结果")
    public BaseResponse getRefund(@ApiParam(value = "交易单号") @RequestParam("tradingOrderNo") Long tradingOrderNo){
        TradingResDTO tradingResDTO = tradingApi.findTradResultByTradingOrderNo(tradingOrderNo);
        return new BaseResponse(tradingResDTO);
    }
}
